#!/bin/bash
set -e

cleanup()
{
    EXIT=$?
    set +e
    echo Stopping rancher server
    kill $PID
    wait $PID
    return $EXIT
}

cd $(dirname $0)/..

#[ "${ARCH}" == "amd64" ] && RACE=-race
go test -cover -tags=test ./pkg/...

if [ ${ARCH} == arm64 ]; then
    export ETCD_UNSUPPORTED_ARCH=arm64
fi

echo Starting rancher server
touch /tmp/rancher.log
./scripts/run >/tmp/rancher.log 2>&1 &
PID=$!
trap cleanup exit

# uncomment to get startup logs. Don't leave them on because it slows drone down too
# much
#tail -F /tmp/rancher.log &
#TPID=$!
while ! curl -sf http://localhost:8080/ping; do
    sleep 2
done
while ! kubectl --kubeconfig /etc/rancher/k3s/k3s.yaml rollout status -w -n cattle-system deploy/rancher-webhook; do
    sleep 2
done
#kill $TPID

echo Running tests

cd ./tests/integration
tox -e rancher -- -m "not nonparallel" -n $(nproc)
tox -e rancher -- -m nonparallel

#tail -f /tmp/rancher-test.log &
